<template>
  <div>
    <!--卡片-->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-button v-hasPermi="['system:generator:add']" type="primary" small class="addBtn" style="margin:10px" size="mini" icon="el-icon-plus" @click="addDialogVisible = true">新增
      </el-button>
      <!--表格-->
      <el-table :data="listForm" stripe border style="width: 100%">
        <el-table-column prop="tableName" label="表名" />
        <el-table-column label="是否配置" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isCfg" size="mini" type="success">已配置</el-tag>
            <el-tag v-else size="mini" type="danger">未配置</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="tableComment" label="表备注" />
        <el-table-column prop="tableCollation" label="字符编码" />
        <el-table-column prop="engine" label="数据库引擎" width="100" />
        <el-table-column prop="createTime" label="创建日期" width="160" />
        <!--操作-->
        <el-table-column label="操作">
          <template slot-scope="scope">

            <el-button size="mini" style="margin-right: 2px" type="text" @click="previewBtn(scope.row.tableName)">
              <el-link type="primary">预览</el-link>
            </el-button>

            <el-button size="mini" style="margin-right: 2px" type="text" @click="cfgBtn(scope.row.tableName)">
              <el-link type="primary">配置</el-link>
            </el-button>

            <el-button size="mini" style="margin-right: 2px" type="text" @click="createBtn(scope.row.tableName)">
              <el-link type="primary">生成</el-link>
            </el-button>

            <el-button size="mini" style="margin-right: 2px" type="text" @click="downloadBtn(scope.row.tableName)">
              <el-link type="primary">下载</el-link>
            </el-button>

          </template>
        </el-table-column>
      </el-table>

    </el-card>
  </div>
</template>

<script>
import { listTable } from '@/api/sys-tools/datasources'
import { create, download } from '@/api/sys-tools/generator'
import { downloadFile } from '@/utils/index'
export default {
  name: 'Datasources',
  data() {
    return {
      listForm: []
    }
  },
  created() {
    // 初始化列表
    this.initList()
  },

  methods: {
    // 预览按钮
    previewBtn(tableName) {
      this.$router.push('/sys-tools/generator/preview/' + tableName)
    },

    // 配置按钮
    cfgBtn(tableName) {
      this.$router.push('/sys-tools/generator/config/' + tableName)
    },

    // 生成按钮
    createBtn(tableName) {
      this.$confirm('生成的代码会在项目对应路劲中，详情看配置里面的包名和模块名?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        create(tableName).then(res => {
          this.$message.success(res.message)
        })
      })
    },

    // 下载按钮
    downloadBtn(tableName) {
      this.$confirm('是否确认下载生成的代码包?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        download(tableName).then(res => {
          if (res.type === 'application/octet-stream') {
            downloadFile(res, tableName, 'zip')
          } else {
            this.$message.error('该表的代码生成器没有配置，请先配置')
          }
        })
      })
    },

    // 初始化数据
    initList() {
      listTable().then(res => {
        this.listForm = res.data
      })
    }
  }
}
</script>

<style scoped>
</style>
